/**
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved. 
 */
package com.wilmar.process.dao.procedure.impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.jdbc.OracleTypes;

import com.wilmar.process.dao.procedure.api.IHistoryProcessInstanceProcedureProxy;
import com.wilmar.process.entity.factory.api.IHistoryProcessInstanceVOFactory;
import com.wilmar.process.entity.query.HistoryProcessInstanceQueryObject;
import com.wilmar.process.entity.vo.HistoryProcessInstanceVO;
import com.wilmar.process.enumerate.EnumDatabaseDataType;
import com.wilmar.process.enumerate.EnumProcedureName;
import com.wilmar.process.exception.ObjectNotFoundException;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0 May 13, 2010
 * @since 1.0.0
 * 
 */
public class HistoryProcessInstanceProcedureProxyImpl extends
		BaseProcedureProxyImpl<HistoryProcessInstanceVO> implements
		IHistoryProcessInstanceProcedureProxy {
	private IHistoryProcessInstanceVOFactory historyProcessInstanceVOFactory;

	/**
	 * @throws ObjectNotFoundException
	 * @see com.wilmar.process.dao.procedure.api.IHistoryProcessInstanceProcedureProxy#getListByQueryObject(com.wilmar.process.entity.query.HistoryProcessInstanceQueryObject)
	 */
	public List<HistoryProcessInstanceVO> getListByQueryObject(
			HistoryProcessInstanceQueryObject qObj)
			throws ObjectNotFoundException {
		Connection con = null;
		List<HistoryProcessInstanceVO> result = null;
		ResultSet rs = null;
		CallableStatement st = null;
		try {
			con = super.getConnectioin();
			String proc = super.generateProcedureCallStatement(
					EnumProcedureName.PROC_HIST_PINST, 10);
			HashMap<String, String> map = new HashMap<String, String>();
			map
					.put(
							HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_DEFINITIONID,
							EnumDatabaseDataType.ARRAY_VARCHAR.getType());
			map
					.put(
							HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_DEFINITIONKEY,
							EnumDatabaseDataType.ARRAY_VARCHAR.getType());
			map
					.put(
							HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_DEFINITIONINAME,
							EnumDatabaseDataType.ARRAY_VARCHAR.getType());
			map
					.put(
							HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_INSTANCEID,
							EnumDatabaseDataType.ARRAY_VARCHAR.getType());
			map
					.put(
							HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_INSTANCEKEY,
							EnumDatabaseDataType.ARRAY_VARCHAR.getType());
			map.put(HistoryProcessInstanceQueryObject.PROPERTY_TASK_INSTANCEID,
					EnumDatabaseDataType.ARRAY_VARCHAR.getType());
			map.put(HistoryProcessInstanceQueryObject.PROPERTY_STATE,
					EnumDatabaseDataType.ARRAY_VARCHAR.getType());
			map.put(HistoryProcessInstanceQueryObject.PROPERTY_OUTSYSTEM,
					EnumDatabaseDataType.ARRAY_VARCHAR.getType());
			map.put(HistoryProcessInstanceQueryObject.PROPERTY_MAX_START_TIME,
					EnumDatabaseDataType.DATE.getType());
			map.put(HistoryProcessInstanceQueryObject.PROPERTY_MIN_START_TIME,
					EnumDatabaseDataType.DATE.getType());
			Map<String, Object> pms = qObj.format(map, con);
			st = con.prepareCall(proc);
			st
					.setArray(
							1,
							(Array) pms
									.get(HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_DEFINITIONID));
			st
					.setArray(
							2,
							(Array) pms
									.get(HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_DEFINITIONKEY));
			st
					.setArray(
							3,
							(Array) pms
									.get(HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_DEFINITIONINAME));
			st
					.setArray(
							4,
							(Array) pms
									.get(HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_INSTANCEKEY));
			st
					.setArray(
							5,
							(Array) pms
									.get(HistoryProcessInstanceQueryObject.PROPERTY_PROCESS_INSTANCEID));
			st.setArray(6, (Array) pms
					.get(HistoryProcessInstanceQueryObject.PROPERTY_STATE));
			st.setArray(7, (Array) pms
					.get(HistoryProcessInstanceQueryObject.PROPERTY_OUTSYSTEM));
			st
					.setDate(
							8,
							(Date) pms
									.get(HistoryProcessInstanceQueryObject.PROPERTY_MIN_START_TIME));
			st
					.setDate(
							9,
							(Date) pms
									.get(HistoryProcessInstanceQueryObject.PROPERTY_MAX_START_TIME));
			st.registerOutParameter(10, OracleTypes.CURSOR);
			st.execute();
			rs = (ResultSet) st.getObject(10);
			result = historyProcessInstanceVOFactory.getValueObject(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (st != null)
					st.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (result == null)
			throw new ObjectNotFoundException("No result for query:"
					+ qObj.toString());
		else
			return result;
	}

	/**
	 * @return the historyProcessInstanceVOFactory
	 */
	public IHistoryProcessInstanceVOFactory getHistoryProcessInstanceVOFactory() {
		return historyProcessInstanceVOFactory;
	}

	/**
	 * @param historyProcessInstanceVOFactory
	 *            the historyProcessInstanceVOFactory to set
	 */
	public void setHistoryProcessInstanceVOFactory(
			IHistoryProcessInstanceVOFactory historyProcessInstanceVOFactory) {
		this.historyProcessInstanceVOFactory = historyProcessInstanceVOFactory;
	}
}
